{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas中的行列转换",
   "id": "a3dcc980c7d83879"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:51:45.800073Z",
     "start_time": "2025-09-15T11:51:44.960233Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day16/file/'"
   ],
   "id": "2190fc9f957eda81",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:54:22.835998Z",
     "start_time": "2025-09-15T11:54:22.823095Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '转换.xlsx')\n",
    "print(df)\n",
    "print(df.dtypes)\n",
    "\n",
    "print(df.T)\n",
    "print(df.T.dtypes)\n",
    "# print(df.transpose())\n",
    "\n",
    "# 使用Numpy转置的方法\n"
   ],
   "id": "38b8df2943bcbb46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名 性别  语文  数学  英语\n",
      "0  丁智敏  女  50  70  53\n",
      "1  李平平  女  73  78  53\n",
      "2  卢海军  男  76  53  80\n",
      "3   王松  男  54  67  67\n",
      "4   王刚  男  78  74  60\n",
      "5   张伊  女  60  63  55\n",
      "6  张荣耀  男  64  63  61\n",
      "姓名    object\n",
      "性别    object\n",
      "语文     int64\n",
      "数学     int64\n",
      "英语     int64\n",
      "dtype: object\n",
      "      0    1    2   3   4   5    6\n",
      "姓名  丁智敏  李平平  卢海军  王松  王刚  张伊  张荣耀\n",
      "性别    女    女    男   男   男   女    男\n",
      "语文   50   73   76  54  78  60   64\n",
      "数学   70   78   53  67  74  63   63\n",
      "英语   53   53   80  67  60  55   61\n",
      "0    object\n",
      "1    object\n",
      "2    object\n",
      "3    object\n",
      "4    object\n",
      "5    object\n",
      "6    object\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:56:38.899489Z",
     "start_time": "2025-09-15T11:56:38.887884Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '转换.xlsx')\n",
    "print(df.index.tolist())\n",
    "\n",
    "print(df.T.index.tolist())\n",
    "\n",
    "print('='*30)\n",
    "print(df.columns.tolist())\n",
    "print(df.T.columns.tolist())"
   ],
   "id": "1cbfe823017f166f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6]\n",
      "['姓名', '性别', '语文', '数学', '英语']\n",
      "==============================\n",
      "['姓名', '性别', '语文', '数学', '英语']\n",
      "[0, 1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T11:58:03.441671Z",
     "start_time": "2025-09-15T11:58:03.433267Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_time = pd.DataFrame({\n",
    "    '2024-01': [100, 200, 300],\n",
    "    '2024-02': [150, 250, 350],\n",
    "    '2024-03': [200, 300, 400]\n",
    "}, index=['产品A', '产品B', '产品C'])\n",
    "\n",
    "print(df_time)\n",
    "\n",
    "print(df_time.T)"
   ],
   "id": "63d1987ef1f3c7e9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     2024-01  2024-02  2024-03\n",
      "产品A      100      150      200\n",
      "产品B      200      250      300\n",
      "产品C      300      350      400\n",
      "         产品A  产品B  产品C\n",
      "2024-01  100  200  300\n",
      "2024-02  150  250  350\n",
      "2024-03  200  300  400\n"
     ]
    }
   ],
   "execution_count": 14
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
